package ppjava12v1.chess.service;

import ppjava12v1.chess.Exceptions.IncorrectPositionException;
import ppjava12v1.chess.desk.*;
import ppjava12v1.chess.entities.games.Coordinate;
import ppjava12v1.chess.entities.games.Game;
import ppjava12v1.chess.enums.FigureType;
import ppjava12v1.chess.enums.Side;

import java.util.HashSet;
import java.util.Set;

public class Move2Service {

    private Desk desk;
    private Cell cell;
    private Figure figure;

    private Set<Coordinate> coordinateSet;


    public Move2Service(Figure figure) {
        this.figure = figure;
    }

    public Move2Service() {
        Game newGame = new Game();
        GameService gameService = new GameService();
        gameService.startGame(newGame);
    }

    protected FigureType getFigureCell(Desk desk, Coordinate coordinate){
       return desk.getFigureFromCell(coordinate.getRow(),coordinate.getCol()).getType();
    }

    public Set<Coordinate> figureMove(Desk desk, Coordinate coordinate) throws IncorrectPositionException {

        this.desk=desk;
        coordinateSet = new HashSet<>();

        int rowMove;
        int tempCol = 0;
        if (desk.getCurrentMove() == Side.WHITE) {
            rowMove = -1;
        } else {
            rowMove = 1;
        }


            switch (getFigureCell(desk,coordinate)) {
                case PAWN:
                   if (coordinate.getRow() != 1 && coordinate.getRow() != 8) {
                        coordinate.setRow(coordinate.getRow() + rowMove);
                       if(getFigureCell(desk,coordinate) == null ){
                           coordinateSet.add(new Coordinate(coordinate.getRow(), coordinate.getCol()));
                       }
                    }
                   if (coordinate.getRow() >= 2 && coordinate.getRow() <= 7) {
                        coordinate.setRow(coordinate.getRow() + rowMove);
                       if(getFigureCell(desk,coordinate) == null ){
                           coordinateSet.add(new Coordinate(coordinate.getRow(), coordinate.getCol()));
                       }
                        coordinateSet.add(new Coordinate(coordinate.getRow(), coordinate.getCol()));
                    }

                    break;
                case ROOK:
                    for (int i = 1; i <= 8; i++) {
                        if (i != coordinate.getRow()) {
                            if (desk.getFigureFromCell(i, coordinate.getCol()) == null){
                                coordinateSet.add(new Coordinate(i, coordinate.getCol()));
                            }else { i=8;}
                        }
                    }
                    for (int y = 1; y <= 8; y++) {
                        if (y != coordinate.getCol()) {
                            if (desk.getFigureFromCell(coordinate.getCol(), y) == null)
                                coordinateSet.add(new Coordinate(coordinate.getRow(), y));
                        }else { y=8;}
                    }
                    break;
                case KNIGHT:
                    for (int i = 1; i <= 8; i++) {
                        if (coordinate.getCol() == i) {
                            tempCol = i;
                        }
                    }

                    if ((coordinate.getRow() + 2) <= 8) {
                        if (desk.getFigureFromCell(coordinate.getRow() + 2, tempCol - 1) == null){
                            coordinateSet.add(new Coordinate(coordinate.getRow() + 2, tempCol - 1));
                        }
                    }
                    if ((coordinate.getRow() + 2) <= 8) {
                        if (desk.getFigureFromCell(coordinate.getRow() + 2, tempCol + 1) == null){
                            coordinateSet.add(new Coordinate(coordinate.getRow() + 2, tempCol + 1));
                        }
                    }


                    if ((coordinate.getRow() + 1) <= 8) {
                        if (tempCol - 2 > 0){
                            if (desk.getFigureFromCell(coordinate.getRow() + 1, tempCol - 2) == null){
                                coordinateSet.add(new Coordinate(coordinate.getRow() + 1, tempCol - 2));
                            }
                        }
                    }
                    if ((coordinate.getRow() + 1) <= 8) {
                        if (desk.getFigureFromCell(coordinate.getRow() + 1, tempCol + 2) == null){
                            coordinateSet.add(new Coordinate(coordinate.getRow() + 1, tempCol + 2));
                        }
                    }


                    if ((coordinate.getRow() - 2) >= 1) {
                        if (desk.getFigureFromCell(coordinate.getRow() - 2, tempCol - 1) == null){
                            coordinateSet.add(new Coordinate(coordinate.getRow() - 2, tempCol - 1));
                        }
                    }
                    if ((coordinate.getRow() - 2) >= 1) {
                        if (desk.getFigureFromCell(coordinate.getRow() - 2, tempCol + 1) == null){
                            coordinateSet.add(new Coordinate(coordinate.getRow() - 2, tempCol + 1));
                        }
                    }


                    if ((coordinate.getRow() - 1) >= 1) {
                        if (tempCol - 2 > 0){
                            if (desk.getFigureFromCell(coordinate.getRow() - 1, tempCol - 2) == null){
                                coordinateSet.add(new Coordinate(coordinate.getRow() - 1, tempCol - 2));
                            }
                        }
                    }
                    if ((coordinate.getRow() - 1) >= 1) {
                        if (desk.getFigureFromCell(coordinate.getRow() - 1, tempCol + 2) == null){
                            coordinateSet.add(new Coordinate(coordinate.getRow() - 1, tempCol + 2));
                        }
                    }
                    break;
                case BISHOP:
                    for (int i = 1; i <= 8; i++) {
                        if (coordinate.getCol() == i) {
                            tempCol = i;
                        }
                    }

                    for (int i = 1; i <= 8; i++) {
                        if ((coordinate.getRow() + i) < 9 && (tempCol + i) < 9) {
                            if (desk.getFigureFromCell(coordinate.getRow(), coordinate.getCol()) == null){
                                coordinateSet.add(new Coordinate(coordinate.getRow() + i, tempCol + i));
                            }else { i=8;}
                        }

                        if ((coordinate.getRow() - i) > 0 && (tempCol + i) < 9) {
                            if (desk.getFigureFromCell(coordinate.getRow(), coordinate.getCol()) == null){
                                coordinateSet.add(new Coordinate(coordinate.getRow() - i, tempCol + i));
                            }else { i=8;}
                        }

                        if ((coordinate.getRow() + i) < 9 && (tempCol - i) > 0) {
                            if (desk.getFigureFromCell(coordinate.getRow(), coordinate.getCol()) == null){
                                coordinateSet.add(new Coordinate(coordinate.getRow() + i, tempCol - i));
                            }else { i=8;}
                        }

                        if ((coordinate.getRow() - i) > 0 && (tempCol - i) > 0) {
                            if (desk.getFigureFromCell(coordinate.getRow(), coordinate.getCol()) == null){
                                coordinateSet.add(new Coordinate(coordinate.getRow() - i, tempCol - i));
                            }else { i=8;}
                        }
                    }

                    break;
                case QUEEN:
                    for (int i = 1; i <= 8; i++) {
                        if (i != coordinate.getRow()) {
                            if (desk.getFigureFromCell(i, coordinate.getCol()) == null){
                                coordinateSet.add(new Coordinate(i, coordinate.getCol()));
                            }else { i=8;}
                        }
                    }

                    for (int y = 1; y <= 8; y++) {
                        if (y != coordinate.getCol()) {
                            if (desk.getFigureFromCell(coordinate.getRow(), y) == null){
                                coordinateSet.add(new Coordinate(coordinate.getRow(), y));
                            } else { y=8;}
                        }
                    }

                    for (int i = 1; i <= 8; i++) {
                        if (coordinate.getCol() == i) {
                            tempCol = i;
                        }
                    }

                    for (int i = 1; i <= 8; i++) {
                        if ((coordinate.getRow() + i) < 9 && (tempCol + i) < 9) {
                            if (desk.getFigureFromCell(coordinate.getRow() + i, tempCol + i) == null){
                                coordinateSet.add(new Coordinate(coordinate.getRow() + i, tempCol + i));
                            } else { i=8;}
                        }

                        if ((coordinate.getRow() - i) > 0 && (tempCol + i) < 9) {
                            if (desk.getFigureFromCell(coordinate.getRow() - i, tempCol + i) == null){
                                coordinateSet.add(new Coordinate(coordinate.getRow() - i, tempCol + i));
                            } else { i=8;}
                        }

                        if ((coordinate.getRow() + i) < 9 && (tempCol - i) > 0) {
                            if (desk.getFigureFromCell(coordinate.getRow() + i, tempCol - i) == null){
                                coordinateSet.add(new Coordinate(coordinate.getRow() + i, tempCol - i));
                            } else { i=8;}
                        }

                        if ((coordinate.getRow() - i) > 0 && (tempCol - i) > 0) {
                            if (desk.getFigureFromCell(coordinate.getRow() - i, tempCol - i) == null){
                                coordinateSet.add(new Coordinate(coordinate.getRow() - i, tempCol - i));
                            } else { i=8;}
                        }
                    }
                    break;
                case KING:
                    for (int i = 1; i <= 8; i++) {

                        if (coordinate.getCol() == i) {
                            tempCol = i;
                        }

                    }
                    if ((coordinate.getRow() + 1) < 9 && (tempCol + 1) < 9) {
                        if (desk.getFigureFromCell(coordinate.getRow() + 1, tempCol + 1) == null)
                            coordinateSet.add(new Coordinate(coordinate.getRow() + 1, tempCol + 1));
                    }
                    if ((coordinate.getRow() + 1) < 9 && (tempCol - 1) > 0) {
                        if (desk.getFigureFromCell(coordinate.getRow() + 1, tempCol - 1) == null)
                            coordinateSet.add(new Coordinate(coordinate.getRow() + 1, tempCol - 1));
                    }
                    if ((coordinate.getRow() - 1) > 0 && (tempCol + 1) < 9) {
                        if (desk.getFigureFromCell(coordinate.getRow() - 1, tempCol + 1) == null)
                            coordinateSet.add(new Coordinate(coordinate.getRow() - 1, tempCol + 1));
                    }
                    if ((coordinate.getRow() - 1) > 0 && (tempCol - 1) > 0) {
                        if (desk.getFigureFromCell(coordinate.getRow() - 1, tempCol - 1) == null)
                            coordinateSet.add(new Coordinate(coordinate.getRow() - 1, tempCol - 1));
                    }

                    if ((coordinate.getRow() + 1) < 9) {
                        if (desk.getFigureFromCell(coordinate.getRow() + 1, coordinate.getCol()) == null)
                            coordinateSet.add(new Coordinate(coordinate.getRow() + 1, coordinate.getCol()));
                    }
                    if ((tempCol - 1) > 0) {
                        if (desk.getFigureFromCell(coordinate.getRow(), tempCol - 1) == null)
                            coordinateSet.add(new Coordinate(coordinate.getRow(), tempCol - 1));
                    }
                    if ((coordinate.getRow() - 1) > 0) {
                        if (desk.getFigureFromCell(coordinate.getRow() - 1, coordinate.getCol()) == null)
                            coordinateSet.add(new Coordinate(coordinate.getRow() - 1, coordinate.getCol()));
                    }
                    if ((tempCol + 1) < 9) {
                        if (desk.getFigureFromCell(coordinate.getRow(), tempCol + 1) == null)
                            coordinateSet.add(new Coordinate(coordinate.getRow(), tempCol + 1));
                    }

                    break;
                default:
                    coordinateSet.add(new Coordinate(coordinate.getRow(), coordinate.getCol()));
                    break;
            }

        return coordinateSet;

    }
}
